/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.jsoup.Jsoup;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: app2022 - QrCode2
 * <p>Powered by webrx On 2022-01-24 09:43:51
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class QrCode2 {
    public static void main(String[] args) throws IOException, WriterException {
        //单词翻译
        var w = JOptionPane.showInputDialog("请输入词汇：");
        var u = String.format("http://www.so.com/s?q=%s", w);
        var t = Jsoup.connect(u).get().select("div[class=mh-translation]").get(0).text();

        //翻译结果生成QrCode
        Map<EncodeHintType, Object> hint = new HashMap<>();
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        BitMatrix bmx = new QRCodeWriter().encode(w.concat(":").concat(t), BarcodeFormat.QR_CODE, 300, 300, hint);
        BufferedImage i = MatrixToImageWriter.toBufferedImage(bmx);
        ImageIO.write(i, "png", new File(w + ".png"));

    }
}
